﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Devonline.Core;
using Microsoft.AspNetCore.Identity;

namespace Devonline.Identity
{
    /// <summary>
    /// 资源, 字符串类型的默认实现
    /// </summary>
    [Table("resource"), DisplayName("资源")]
    public class Resource : Resource<string>
    {
        /// <summary>
        /// 父级资源
        /// </summary>
        public virtual Resource Parent { get; set; }
        /// <summary>
        /// 资源级别
        /// </summary>
        public virtual Level Level { get; set; }
    }

    /// <summary>
    /// 资源的定义
    /// </summary>
    [Table("resource"), DisplayName("资源")]
    public class Resource<TKey> : Identity<TKey> where TKey : IConvertible
    {
        /// <summary>
        /// 父级资源编号
        /// </summary>
        [Column("parent_id"), DisplayName("父级资源编号"), MaxLength(36), Excel]
        public virtual TKey ParentId { get; set; }
        /// <summary>
        /// 资源所有者编号
        /// </summary>
        [Column("owner_id"), DisplayName("资源所有者"), Required, MaxLength(36), Excel]
        public virtual TKey OwnerId { get; set; }
        /// <summary>
        /// 资源所有者类型 AssignType 枚举值
        /// </summary>
        [Column("assign_type", TypeName = "VARCHAR(16)"), DisplayName("资源所有者类型"), DefaultValue(AssignType.User), Excel]
        public virtual AssignType AssignType { get; set; }
        /// <summary>
        /// 资源类型 ResourceType 枚举类型的值
        /// </summary>
        [Column("resource_type", TypeName = "VARCHAR(16)"), DisplayName("资源类型"), DefaultValue(ResourceType.Page), Excel]
        public virtual ResourceType ResourceType { get; set; }
        /// <summary>
        /// 资源标题
        /// </summary>
        [Column("title"), DisplayName("资源标题"), MaxLength(36), Excel]
        public virtual string Title { get; set; }
        /// <summary>
        /// 资源保存的具体内容, 可能是密文
        /// </summary>
        [ProtectedPersonalData]
        [Column("content"), DisplayName("资源内容"), MaxLength(1024), Excel]
        public virtual string Content { get; set; }
        /// <summary>
        /// 资源定义的可访问级别 AccessLevel 枚举类型的值
        /// </summary>
        [Column("access_level", TypeName = "VARCHAR(16)"), DisplayName("访问级别"), DefaultValue(AccessLevel.Internal), Excel]
        public virtual AccessLevel AccessLevel { get; set; }
        /// <summary>
        /// 资源级别, 即资源定义的最低访问级别
        /// </summary>
        [Column("level_id"), DisplayName("资源级别"), MaxLength(36), Excel]
        public virtual TKey LevelId { get; set; }
    }
}
